files = dir('*.tif');
files.name

flist = []; 
for i = 1:numel(files) 
    flist = [flist; files(i).name];
end
flist = sort(flist, 'ascend')

%%
coefile = 'new_icons.coe';
delete(coefile);
fid = fopen(coefile,'w');
fprintf(fid,'MEMORY_INITIALIZATION_RADIX=16;\n');
fprintf(fid,'MEMORY_INITIALIZATION_VECTOR=\n');

for k = 1:numel(files)
im_name = files(k).name;
im = imread(im_name);
im = im(:,:,1:3);

im2 = im(:,:,1);

h  = im2 > 199;
m  = im2 < 199 & im2 > 50;
l  = im2 < 50;
a  = im2 == 199;

im2(h) = 255;
im2(m) = 136;
im2(l) = 0;
im2(a) = 199;

[~,filename] = fileparts(im_name);
for i = 1:size(im2,1)
    str = '';
    for j = 1:size(im2,2)
        switch (im2(i,j))
            case 199
                str = [str '0,'];
            case 136
                str = [str '1,'];
            case 0
                str = [str '2,'];                
            case 255
                str = [str '3,'];
        end
    end
    fprintf(fid,'%s\n',str);
end
image(im2); colormap(gray)
end
fclose(fid)
